package com.wangtianxiang.sxp.common.utils;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.Result;
import com.baidu.unbiz.fluentvalidator.ValidationError;
import com.baidu.unbiz.fluentvalidator.jsr303.HibernateSupportedValidator;

import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.List;

import static com.baidu.unbiz.fluentvalidator.ResultCollectors.toComplex;
import static com.baidu.unbiz.fluentvalidator.ResultCollectors.toSimple;

/**
 * 流式校验工具
 *
 */
public class FluentValidateUtils {
	private static final   ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
	
	private FluentValidateUtils(){
		
	}
	/**
	 * 简单校验结果</br>
	 * 支持 hibernate-validator 和fluent-validate 校验
	 * @param o 校验对象 
	 * @return
	 */
	public static <T> Result toSimpleValidate(T o){
		return toSimpleValidate(o, null, null);
	}
	/**
	 * 简单校验结果</br>
	 * 支持 hibernate-validator 和fluent-validate 校验
	 * @param o 校验对象 
	 * @param groups 校验组
	 * @param excludeGroups 排除校验组
	 * @return
	 */
	public static <T> Result toSimpleValidate(T o,Class<?>[] groups,Class<?>[] excludeGroups){
		Validator validator = factory.getValidator();
		Result result = FluentValidator.checkAll(groups).setExcludeGroups(excludeGroups).failFast()
				.on(o)
	            .on(o, new HibernateSupportedValidator<T>().setHiberanteValidator(validator))
	           .doValidate().result(toSimple());
		return result;
	}
	/**
	 * 复杂校验结果</br>
	 * 支持 hibernate-validator 和fluent-validate 校验
	 * @param o 校验对象
	 * @return
	 */
	public static <T> ComplexResult toComplexValidate(T o){
		return toComplexValidate(o, null, null);
	}
	/**
	 * 复杂校验结果</br>
	 * 支持 hibernate-validator 和fluent-validate 校验
	 * @param o 校验对象 
	 * @param groups 校验组
	 * @param excludeGroups 排除校验组
	 * @return
	 */
	public static <T> ComplexResult toComplexValidate(T o,Class<?>[] groups,Class<?>[] excludeGroups){
		Validator validator = factory.getValidator();
		ComplexResult complexResult = FluentValidator.checkAll(groups).setExcludeGroups(excludeGroups).failFast()
				.on(o)
	            .on(o, new HibernateSupportedValidator<T>().setHiberanteValidator(validator)).doValidate().result(toComplex());
		return complexResult;
	}
	
	public static String forErrorString(Result result){
		List<String> errors=  result.getErrors();
		String str = null;
		if(null!= errors&& !errors.isEmpty()){
			str = "";
			for(int i =0 ;i<errors.size();i++){
				str+=errors.get(i);
				if(i<errors.size()-1){
					str+=",";
				}
			}
		}
		return str;
	}
	public static String forErrorString(ComplexResult result){
		List<ValidationError> errors=  result.getErrors();
		String str = null;
		if(null!= errors&& !errors.isEmpty()){
			str = "";
			for(int i =0 ;i<errors.size();i++){
				str+=errors.get(i).getField()+":"+errors.get(i).getErrorMsg();
				if(i<errors.size()-1){
					str+=",";
				}
			}
		}
		return str;
	}	
	
}
